package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.userInfo.entity.CollectedArticle;
import com.rongshu.view.userInfo.listener.OnCorpusItemClickListener;
import com.rongshu.widget.RoundImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectedArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //context
    private Context mContext = null;
    //list
    private List<CollectedArticle.ResponseBean> collectedArticles = null;
    //interface
    private OnCorpusItemClickListener mOnCorpusItemClickListener = null;

    public CollectedArticleAdapter(Context context) {
        this.mContext = context;
    }

    public void setCollectedArticles(List<CollectedArticle.ResponseBean> list) {
        this.collectedArticles = list;
    }

    public void addCollectedArticles(List<CollectedArticle.ResponseBean> list) {
        this.collectedArticles.addAll(list);
    }

    public List<CollectedArticle.ResponseBean> getCollectedArticles() {
        return this.collectedArticles;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_collected_article_item_view, parent, false);
        return new CollectedArticleViewHolder(view);
    }

    /**
     * is_check :1.已发布,2.审核中,3.草稿,4.退稿
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CollectedArticle.ResponseBean bean = this.collectedArticles.get(position);
        CollectedArticleViewHolder viewHolder = (CollectedArticleViewHolder) holder;
        //image
        final String topImg = bean.getTopImg();
        if (TextUtils.isEmpty(topImg)) {
            viewHolder.article_img.setVisibility(View.GONE);
        } else {
            viewHolder.article_img.setVisibility(View.VISIBLE);
            GlideUtils.loadImageView(mContext, viewHolder.article_img, topImg,
                    R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        }
        //title
        viewHolder.article_title.setText(bean.getTitle());
        //content
        viewHolder.article_content.setText(bean.getContents_header());
        //状态
        final int is_check = bean.getIs_check();
        //信息
        String info = is_check == 1 || is_check == 2 ? DateUtils.ConvertDate(bean.getCreate_datetime())
                : is_check == 3 ? "字数" + bean.getNumber() + "   " + DateUtils.ConvertDate(bean.getCreate_datetime())
                : is_check == 4 ? "退稿 字数" + bean.getNumber() + "   " + DateUtils.ConvertDate(bean.getCreate_datetime()) : "";
        viewHolder.txt_article_info.setText(info);
        //编辑的显示与隐藏
        viewHolder.btn_edit.setVisibility(is_check == 1 || is_check == 2 ? View.GONE : View.VISIBLE);
        //item
        viewHolder.layout_collected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCorpusItemClickListener != null) {
                    mOnCorpusItemClickListener.onClick(is_check, bean.getContribute_id(),
                            bean.getTitle(), bean.getContents_header(), topImg);
                }
            }
        });
        //编辑
        viewHolder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (is_check == 3 || is_check == 4) {
                    if (mOnCorpusItemClickListener != null) {
                        mOnCorpusItemClickListener.onEditClick(bean.getContribute_id(), bean.getTitle(),
                                bean.getContents_header(), topImg);
                    }
                }
            }
        });
        //删除
        viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCorpusItemClickListener != null) {
                    mOnCorpusItemClickListener.onItemDeleteClick(position);
                }
            }
        });

        viewHolder.btn_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCorpusItemClickListener != null) {
                    mOnCorpusItemClickListener.onMoveClick(position, bean.getContribute_id());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return collectedArticles == null ? 0 : collectedArticles.size();
    }

    public class CollectedArticleViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_collected)
        LinearLayout layout_collected;
        @BindView(R.id.article_img)
        ImageView article_img;
        @BindView(R.id.article_title)
        TextView article_title;
        @BindView(R.id.article_content)
        TextView article_content;
        @BindView(R.id.txt_article_info)
        TextView txt_article_info;
        @BindView(R.id.btn_move)
        TextView btn_move;
        @BindView(R.id.btn_edit)
        TextView btn_edit;
        @BindView(R.id.btn_delete)
        TextView btn_delete;

        public CollectedArticleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //set listener
    public void setOnCorpusItemClickListener(OnCorpusItemClickListener listener) {
        mOnCorpusItemClickListener = listener;
    }
}
